package cn.kt.ipcount.chatgpt;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by tao.
 * Date: 2023/2/9 16:06
 * 描述:
 */
public class ChatGptUtil {

    public static Object ChatGptSend(String prompt){
        OpenAiConfig openAiConfig = new OpenAiConfig();
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json;charset=UTF-8");

        JSONObject json = new JSONObject();
        //选择模型
        json.set("model", openAiConfig.getModel());
        //添加我们需要输入的内容
        json.set("prompt", prompt);
        json.set("temperature", openAiConfig.getTemperature());
        json.set("max_tokens", openAiConfig.getMax_tokens());
        json.set("top_p", openAiConfig.getTop_p());
        json.set("frequency_penalty", openAiConfig.getFrequency_penalty());
        json.set("presence_penalty", openAiConfig.getPresence_penalty());

        HttpResponse response = HttpRequest.post(openAiConfig.getUrl())
                .headerMap(headers, false)
                .bearerAuth(openAiConfig.getTkoen())
                .body(String.valueOf(json))
                .timeout(5 * 60 * 1000)
                .execute();
        return response.body();
    }
}
